

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 当期播放歌曲
    currentMusic: {
      picUrl: "http://p2.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg"
    },
    // 播放列表
    playMusicList: [],
    // 播放状态
    isPlay: false,
    // 播放顺序
    succession: "顺序",
    // 歌曲当前播放时间
    currentTime:0,
    // 音量
    volume:0,
    // 时长
    duration:0,
    // 在详情页点改变播放时间
    changeCurrentTime:0,
    // 点击的图片itme
    listItem:{},
  },
  getters: {
    oneSong(state) {
      return state.playMusicList[0]
    },
  },
  mutations: {
    // 获取点击图片相应的item
    GETITEM(state,val){
      state.listItem=val
    },
    // 获取改变歌曲进度的时间
    GETCHANGECURRENTIME(state,val){
      state.changeCurrentTime=val
    },
    // 获取时长
    GETDURATION(state,val){
      state.duration=val
    },
    // 获取音量
    GETVOLUME(state,val){
      state.volume=val
    },
    // 更新当期播放歌曲
    UPDATACURENTMUSIC(state, val) {
      state.currentMusic = val
    },
    // 更新播放列表
    UPDATAPLAYMUSICLIST(state, val) {
      state.playMusicList.unshift(val)
      // 去重
      for (let i = 0; i < state.playMusicList.length; i++) {
        for (let j = i + 1; j < state.playMusicList.length; j++) {
          if (state.playMusicList[i] === state.playMusicList[j]) {
            state.playMusicList.splice(j, 1)
            j--
          }
        }
      }
    },
    //更新播放顺序
    UPDATASUCCESSION(state, val) {
      state.succession = val
    },
    // 更新播放状态
    UPDATAISPLAY(state) {
      setTimeout(() => {
        state.isPlay = !state.isPlay
      }, 100);
    },
    // 顺序上一首
    UPONE(state) {
      if (state.currentMusic.id == state.playMusicList[0].id) {
        let index = state.playMusicList.length - 1
        state.currentMusic = state.playMusicList[index]
      } else {
        let index = state.playMusicList.findIndex(
          (item) => state.currentMusic.id == item.id
        )
        state.currentMusic = state.playMusicList[index - 1];
      }
    },
    // 顺序下一首
    NEXTONE(state) {
      let index = state.playMusicList.length - 1
      if (state.currentMusic.id == state.playMusicList[index].id) {
        state.currentMusic = state.playMusicList[0]
      } else {
        let index = state.playMusicList.findIndex(
          (item) => state.currentMusic.id == item.id
        );
        state.currentMusic = state.playMusicList[index + 1]
      }
    },
    // 随机下一首
    SUINEXTONE(state) {
      let index = state.playMusicList.findIndex(item => state.currentMusic == item)
      for (let i = Math.floor(Math.random() * state.playMusicList.length); i <= state.playMusicList.length;) {
        if (state.playMusicList[i] != state.playMusicList[index]) {
          state.currentMusic = state.playMusicList[i]
          return
        } else {
          return
        }
      }
    },
    // 单曲循环
    DANNEXTONE(state) {
      let index = state.playMusicList.findIndex(item => state.currentMusic == item)
      state.currentMusic = []
      state.currentMusic = state.playMusicList[index]
    },
    // 在外面切换上一首
    OUTUP(state) {
      if (state.succession == "顺序") {
        this.commit("UPONE")
      } else if (state.succession == "随机") {
        this.commit("SUINEXTONE")
      } else {
        this.commit("DANNEXTONE")
      }
    },
    // 在外面切换下一首
    OUTNEXT(state) {
      if (state.succession == "顺序") {
        this.commit("NEXTONE")
      } else if (state.succession == "随机") {
        this.commit("SUINEXTONE")
      } else {
        this.commit("DANNEXTONE")
      }
    },
    // 删除歌曲列表中相应的歌曲
    DELSONG(state, index) {
      if (state.playMusicList.length > 1) {
        state.playMusicList.splice(index, 1)
      } else {
        return
      }
    },
    // 歌曲列表变更时当前播放歌曲永远指向列表第一首
    UPDATAONESONG(state) {
      state.currentMusic = state.playMusicList[0]
    },
    // 点击歌曲列表切换歌曲
    CHANGESONG(state, item) {
      let index = state.playMusicList.findIndex(i => i.id == item.id)
      // let index = state.playMusicList.indexOf(item)
      state.currentMusic = state.playMusicList[index]
    },
    // 获取歌曲播放时间
    GETCURRENTIME(state,currentTime){
      state.currentTime=currentTime
      // console.log(state.currentTime);
    },
  },
  actions: {
  },
  modules: {
  }
})
